using System;
using System.Runtime.CompilerServices;
using DG.Tweening;
using LP;
using UnityEngine;
using UnityEngine.UI;

namespace Logic.Launch.UI
{
    [UI("LaunchPanel", EUIType.BaseCanvas, false, true)]
    public sealed class LaunchPanel : UIBase
    {
        private Slider sliderLoading;
        private Text txtHint;

        public override void OnInit()
        {
            base.OnInit();
            sliderLoading = RootTrans.GetCom<Slider>("sliderLoading");
            txtHint = RootTrans.GetCom<Text>("txtHint");

            sliderLoading.value = 0;
            sliderLoading.onValueChanged.AddListener(OnSliderLoadingChanged);

            Global.UIEventHandler.RegHandler(ELaunch.OnUpdateLoadingProgress, OnUpdateLoadingProgress,
                TokenSource.Token);
            Global.UIEventHandler.RegHandler(ELaunch.OnUpdateLoadingHintText, OnUpdateLoadingHintText,
                TokenSource.Token);
        }

        private void OnSliderLoadingChanged(float value)
        {
            if (value >= 1)
            {
                Debug.Log("加载完毕");
                Global.SceneEventHandler.Notify(EScene.ShowBattleScene);
            }
        }

        private void OnUpdateLoadingProgress(ITuple obj)
        {
            float progress = ((ValueTuple<float>) obj).Item1;
            sliderLoading.DOValue(progress, 0.5f).SetEase(Ease.Linear);
        }

        private void OnUpdateLoadingHintText(ITuple obj)
        {
            string hintStr = ((ValueTuple<string>) obj).Item1;
            txtHint.text = hintStr;
        }

        public LaunchPanel(int id, string uiPath, bool isCache, Transform rootTrans, EUIType uIType) : base(id, uiPath,
            isCache, rootTrans, uIType)
        {
        }
    }
}